package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserTravel;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserTravelDaoImpl extends TempDataObjectDao<UserTravel> {

	public UserTravel getUserTravel(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_travel where userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTravel data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserTravel data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_travel(userId,cityId,travelTimes,modifyTime,totalTravelTimes,totalBeautyEncounterTimes,travelEventTimesData,"
				+ "power,lastRestoreTime,multiChildTypeData,eventIdData,beautyTrystData,totalCasinoTimes,todayEventTimesData) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getCityId());
			call.setInt(i++, data.getTravelTimes());
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getTotalTravelTimes());
			call.setLong(i++, data.getTotalBeautyEncounterTimes());
			call.setString(i++, data.getTravelEventTimesMapJsonString());
			call.setInt(i++, data.getPower());
			call.setLong(i++, data.getLastRestoreTime());
			call.setString(i++, StringUtils.listToString(data.getMultiChildTypeList(), ","));
			call.setString(i++, StringUtils.listToString(data.getEventIdList(), ","));
			call.setString(i++, StringUtils.listToString(data.getBeautyTrystList(), ","));
			call.setLong(i++, data.getTotalCasinoTimes());
			call.setString(i++, data.getTodayEventTimesMapJsonString());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTravel data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_travel SET cityId=?,travelTimes=?,modifyTime=?,totalTravelTimes=?,totalBeautyEncounterTimes=?,travelEventTimesData=?,"
				+ "power=?,lastRestoreTime=?,multiChildTypeData=?,eventIdData=?,beautyTrystData=?,totalCasinoTimes=?,todayEventTimesData = ? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getCityId());
			call.setInt(i++, data.getTravelTimes());
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getTotalTravelTimes());
			call.setLong(i++, data.getTotalBeautyEncounterTimes());
			call.setString(i++, data.getTravelEventTimesMapJsonString());
			call.setInt(i++, data.getPower());
			call.setLong(i++, data.getLastRestoreTime());
			call.setString(i++, StringUtils.listToString(data.getMultiChildTypeList(), ","));
			call.setString(i++, StringUtils.listToString(data.getEventIdList(), ","));
			call.setString(i++, StringUtils.listToString(data.getBeautyTrystList(), ","));
			call.setLong(i++, data.getTotalCasinoTimes());
			call.setString(i++, data.getTodayEventTimesMapJsonString());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTravel parseData(ResultSet rs) throws SQLException {
		UserTravel data = new UserTravel();
		data.setUserId(rs.getLong("userId"));
		data.setCityId(rs.getInt("cityId"));
		data.setTravelTimes(rs.getInt("travelTimes"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setTotalTravelTimes(rs.getLong("totalTravelTimes"));
		data.setTotalBeautyEncounterTimes(rs.getLong("totalBeautyEncounterTimes"));
		data.setPower(rs.getInt("power"));
		data.setLastRestoreTime(rs.getLong("lastRestoreTime"));
		data.setTotalCasinoTimes(rs.getLong("totalCasinoTimes"));
		data.setMultiChildTypeList(StringUtils.stringToIntegerLinkedListNoZero(rs.getString("multiChildTypeData"), ","));
		data.setEventIdList(StringUtils.stringToIntegerLinkedListNoZero(rs.getString("eventIdData"), ","));
		data.setBeautyTrystList(StringUtils.stringToIntegerLinkedListNoZero(rs.getString("beautyTrystData"), ","));
		String travelEventTimesData = rs.getString("travelEventTimesData");
		Map<Integer, Long> travelEventTimesMap;
		if(StringUtils.isNullOrEmpty(travelEventTimesData)){
			travelEventTimesMap = new ConcurrentHashMap<>();
		}else {
			travelEventTimesMap = JSON.parseObject(travelEventTimesData, new TypeReference<Map<Integer, Long>>() {});
		}
		data.setTravelEventTimesMap(travelEventTimesMap);

		String todayEventTimesDataStr = rs.getString("todayEventTimesData");
		Map<Integer, Long> todayEventMap;
		if(StringUtils.isNullOrEmpty(todayEventTimesDataStr)){
			todayEventMap = new ConcurrentHashMap<>();
		}else {
			todayEventMap = JSON.parseObject(todayEventTimesDataStr, new TypeReference<Map<Integer, Long>>() {});
		}
		data.setTodayEventTimesMap(todayEventMap);

		return data;
	}
}
